//
//  StoreCollectionVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2018/1/8.
//  Copyright © 2018年 LJL. All rights reserved.
//

#import "StoreCollectionVC.h"
#import "StoreReusableView.h"
#import "StoreCollectionViewCell.h"
#import "ArtworkCollectionViewCell.h"
#import "IGSearchVC.h"
#import "StoreDeatilVC.h"
#import "GoodsDetailVC.h"
#import "StoreHomeApi.h"
#import "UICollectionViewLeftAlignedLayout.h"
@interface StoreCollectionVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)StoreHomeModel *model;
@property (nonatomic, strong)StoreHomeApi *api;
@property (nonatomic, strong)UICollectionView *collectionView;
@end

@implementation StoreCollectionVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setNav];
    self.api = [StoreHomeApi new];
    self.model = [StoreHomeModel new];
    [self loadData];
    [self loadUI];
}

- (void)setNav
{
    self.title = @"商城";
    [self ig_setupRightButtonItemWithTitle:nil image:[UIImage imageNamed:@"app_search_icon"] highLightedImage:[UIImage imageNamed:@"app_search_icon"]];
}

- (void)ig_rightButtonPressed:(id)sender
{
    IGSearchVC *storeSearchVC = [[IGSearchVC alloc] init];
    storeSearchVC.type = IGHotKeyTypeStore;
    [UIViewController ig_pushViewController:storeSearchVC animated:YES];
}

- (void)loadUI
{
    UICollectionViewLeftAlignedLayout *leftAlignedLayout = [[UICollectionViewLeftAlignedLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH,KSCREENHEIGHT) collectionViewLayout:leftAlignedLayout];
    self.collectionView.backgroundColor = [UIColor c2Color];
    self.collectionView.alwaysBounceVertical = YES;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"StoreCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"StoreCollectionViewCell"];
    [self.collectionView registerClass:[ArtworkCollectionViewCell class] forCellWithReuseIdentifier:@"ArtworkCollectionViewCell"];
    [self.collectionView registerClass:[StoreReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"StoreReusableView"];
    @KWeakObj(self);
    [self.collectionView ig_setHeaderWithRefreshingBlock:^{
        [selfWeak loadData];
    }];
    [self.view addSubview:self.collectionView];
}

- (void)loadData
{
    @KWeakObj(self);
    [self.api startWithDecodeClass:[StoreHomeResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            [selfWeak.collectionView ig_endHeaderRefreshing];
            StoreHomeResp *resp = responseObject;
            _model = resp.data;
            [_collectionView reloadData];
        }
    }];
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.model.goodsAllList.count > 0 ? self.model.goodsAllList.count : 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    StoreListModel *listModel = _model.goodsAllList[section];
    if (listModel.childTypeList && listModel.childTypeList.count > 0)
    {
        return listModel.goodsList.count + 1;
    }
    else
    {
        return listModel.goodsList.count ? : 0;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoreListModel *listModel = _model.goodsAllList[indexPath.section];
    if (listModel.childTypeList.count > 0)
    {
        if (indexPath.row == 0)
        {
            return CGSizeMake(KSCREENWIDTH, 40);
        }
        else
        {
            CGFloat width = (KSCREENWIDTH - 4) / 2;
            CGFloat height = width * 360 / 372;
            return CGSizeMake(width , height + 65);
        }
    }
    else
    {
        CGFloat width = (KSCREENWIDTH - 3) / 2;
        CGFloat height = width * 360 / 372;
        return CGSizeMake(width , height + 65);
    }
    return CGSizeZero;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoreListModel *listModel = _model.goodsAllList[indexPath.section];
    if (listModel.childTypeList.count > 0)
    {
        if (indexPath.item == 0)
        {
            ArtworkCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ArtworkCollectionViewCell" forIndexPath:indexPath];
            cell.dataArr = [listModel.childTypeList mutableCopy];
            return cell;
        } else {
            StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCollectionViewCell" forIndexPath:indexPath];
            GoodsListModel *goodsModel = listModel.goodsList[indexPath.item - 1];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:goodsModel.image]];
            cell.storeName.text = goodsModel.title;
            cell.priceL.text = [NSString stringWithFormat:@"¥%@", goodsModel.price];
            return cell;
        }
    } else {
    StoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StoreCollectionViewCell" forIndexPath:indexPath];
        GoodsListModel *goodsModel = listModel.goodsList[indexPath.item];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:goodsModel.image]];
        cell.storeName.text = goodsModel.title;
        cell.priceL.text = [NSString stringWithFormat:@"¥%@", goodsModel.price];
    return cell;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    StoreListModel *listModel = _model.goodsAllList[indexPath.section];
    if (listModel.childTypeList.count > 0)
    {
        if (indexPath.item != 0)
        {
            GoodsListModel *goodsModel = listModel.goodsList[indexPath.item - 1];
            GoodsDetailVC *vc = [[GoodsDetailVC alloc] init];
            vc.goodsId = goodsModel.aId;
            [UIViewController ig_pushViewController:vc animated:YES];
        }
    }
    else
    {
        GoodsListModel *goodsModel = listModel.goodsList[indexPath.item];
        GoodsDetailVC *vc = [[GoodsDetailVC alloc] init];
        vc.goodsId = goodsModel.aId;
        [UIViewController ig_pushViewController:vc animated:YES];
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(3, 0, 3, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 3;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(KSCREENWIDTH, 40);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader)
    {
        StoreListModel *listModel = _model.goodsAllList[indexPath.section];
        StoreReusableView *resV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"StoreReusableView" forIndexPath:indexPath];
        resV.block = ^(UIButton *btn) {
            StoreDeatilVC *vc = [[StoreDeatilVC alloc] init];
            vc.str = listModel.parentTitle;
            vc.type = @"1";
            vc.value = listModel.parentId;
            [UIViewController ig_pushViewController:vc animated:YES];
        };
        [resV setTitleL:listModel.parentTitle];
        return resV;
    }
    return nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}



@end
